package com.part2.action.function;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.part2.Constants;
import com.part2.action.common.CommonAction;
import com.part2.action.common.RandomGUID;
import com.part2.action.common.form.CustomActionForm;
import com.part2.action.common.form.ValueObject;
import com.part2.action.common.validator.Validator;
import com.part2.beans.Bigclass;
import com.part2.beans.Products;
import com.part2.beans.Smallclass;
import com.part2.business.function.BigClassService;
import com.part2.business.function.ProductsService;
import com.part2.business.function.SmallclassService;
import com.part2.common.query.Page;

public class SmallClassAction extends CommonAction<Smallclass> {
	private final Log logger = LogFactory.getLog(getClass());
	private SmallclassService smallclassService;
	private ProductsService productsService;
	private static BigClassService bigClassService;

	public void setBigClassService(BigClassService bigClassService) {
		this.bigClassService = bigClassService;
	}

	public void setsmallClassService(SmallclassService smallclassService) {
		this.smallclassService = smallclassService;
	}

	public void setProductsService(ProductsService productsService) {
		this.productsService = productsService;
	}

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		logger.info("::::::::::SmallClass-List");
		Page<Smallclass> p = initPage(request, Constants.PRE_PAGE_SIZE);// 设置每页的记录数
		Page<Smallclass> page = smallclassService.findSmallclass(p);
		List<Bigclass> bClassList = bigClassService.findBigClass();
		request.setAttribute("blist", bClassList);
		if (page != null) {
			request.setAttribute("page", page);
		} else {
			request.setAttribute("res", "暂无记录！");
		}
		logger.info("::::::::::SmallClass-List End");
		return mapping.findForward("smallClassList");
	}

	public ActionForward addSmallClass(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		logger.info("::::::::::SmallClass-Add");
		ValueObject sVo = ((CustomActionForm) form).getVoa().getValue("small");
		String id = new RandomGUID() + "sclass";
		String name = sVo.getField("name");
		String nameEn = sVo.getField("nameEn");
		String order = sVo.getField("order");
		String parentId = sVo.getField("parent");
		Smallclass sclass = new Smallclass();
		sclass.setSclassId(id);
		sclass.setSclassName(name);
		sclass.setSclassNameEn(nameEn);
		sclass.setOrderS(Short.parseShort(order));
		sclass.setBclassId(parentId);
		smallclassService.addSmallclass(sclass);
		logger.info("::::::::::SmallClass-Add End");
		return list(mapping, form, request, response);
	}

	public ActionForward editSmallClass(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		logger.info("::::::::::SmallClass-Edit");
		ValueObject sVo = ((CustomActionForm) form).getVoa().getValue("edit");
		String id = sVo.getField("id");
		if (Validator.isNotNull(id)) {
			Smallclass sm = smallclassService.findSmallclassByPrimeKey(id);
			sm.setSclassName(sVo.getField("name"));
			sm.setSclassNameEn(sVo.getField("nameEn"));
			sm.setOrderS(Short.parseShort(sVo.getField("order")));
			sm.setBclassId(sVo.getField("parent"));
			smallclassService.updateSmallclass(sm);
		}
		logger.info("::::::::::SmallClass-Edit End");
		return list(mapping, form, request, response);
	}

	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		logger.info("::::::::::SmallClass-Delete");
		String id = request.getParameter("sclassId");
		if (Validator.isNotNull(id)) {
			List<Products> p = smallclassService.findSmallClassByProductsId(id);
			if (p != null && p.size() > 0) {
				response.setContentType("text/html;charset=UTF-8");
				response.getWriter().write(
						"<script language='javascript'>alert('"
								+ "删除失败!请将该类下产品删除后再删除!"
								+ "');history.go(-1); </script>");
				return null;
			} else {
				smallclassService.deleteSmallclass(id);
				logger.info("::::::::::delete " + id + " success");
				return this.list(mapping, form, request, response);
			}
		}
		return this.list(mapping, form, request, response);
	}

}
